home *** CD-ROM | disk | FTP | other *** search
/ Amiga Games: Greatest Hits 1996 / Amiga Games: Greatest Hits 1996.iso / archive / userbox / publicdomain / frexxed.lha / frexxed / bin / fwrap.c < prev    next >
C/C++ Source or Header  |  1994-10-14  |  4KB  |  212 lines

  1. /*
  2.  * Wrapper - A word wrap filter.
  3.  *
  4.  * Adds a custum string to each line and wraps the words that get outside
  5.  * the wrap column.
  6.  *
  7.  * Reads from stdin, writes output to stdout.
  8.  *
  9.  * Author: Daniel Stenberg
  10.  * Date: 941012
  11.  */
  12.  
  13. #include <string.h>
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <ctype.h>
  17.  
  18. #define TRUE  1
  19. #define FALSE 0
  20.  
  21. #define WRAP_STRING "> "
  22. #define WRAP_COLUMN 79
  23. #define WRAP_FILEBUFFER_SIZE 1024
  24.  
  25. int Wrap(void);
  26. int Pipe(char *);
  27. int Puts(char *);
  28. int WhiteCount(char *);
  29. int WordCount(char *);
  30. int PutNchar(char *, int);
  31. int Putchar(int);
  32.  
  33. static int column; /* right border column */
  34. static int currcol; /* current column */
  35. static char *leftside; /* left side string */
  36.  
  37. static char shutoff = FALSE; /* shutoff the 'fancy' option ? */
  38.  
  39. int main(int argc, char *argv[])
  40. {
  41.     int ret;
  42.     column = WRAP_COLUMN;
  43.     leftside = WRAP_STRING;
  44.     if(argc>1) {
  45.         int n=1;
  46.         while(n<argc) {
  47.             if(argv[n][0] == '-') {
  48.                 switch(argv[n][1]) {
  49.                   case 's':
  50.                     if(n+1<argc)
  51.                         leftside = argv[n+1];
  52.                     n+=2;
  53.                     break;
  54.                   case 'c':
  55.                     if(n+1<argc)
  56.                         column = atoi(argv[n+1]);
  57.                     n+=2;
  58.                     break;
  59.                   case 'q':
  60.                     shutoff = TRUE;
  61.                     n++;
  62.                     break;
  63.                   default:
  64.                     printf("Usage: wrap [-scq] <input >output\n\n"
  65.                            "-s [string]   Left side string\n"
  66.                            "-c [column]   Word wrap column\n"
  67.                            "-q            Shut off the 'fancy' wrapping\n");
  68.                     return 0;
  69.                 }
  70.             }
  71.         }
  72.     }
  73.  
  74.     if(column < strlen(leftside) + 10) {
  75.         printf("*** ABORTED!\nStupid wrap column/string argument!\n");
  76.         return 0;
  77.     }
  78.  
  79.     ret = Wrap();
  80.  
  81.     return ret;
  82. }
  83.  
  84. int Wrap(void)
  85. {
  86.     int ret=0;
  87.     char buffer[WRAP_FILEBUFFER_SIZE];
  88.     int len;
  89.     int prevlen=0;
  90.     int linenum=0;
  91.     int newlines=0;
  92.     buffer[0]=0;
  93.     while(fgets(buffer, WRAP_FILEBUFFER_SIZE, stdin)) {
  94.         linenum++;
  95.         len = strlen(buffer);
  96.         if(buffer[len-1] == '\n') {
  97.             /* remove trailing newline character */
  98.             buffer[len-1] = 0;
  99.         }
  100.         if(isspace(buffer[0]) || len==1) {
  101.             if(linenum>1) {
  102.                 /* This is the beginning of a new paragraph. */
  103.                 Putchar('\n');
  104.                 newlines++;
  105.             }
  106.         }
  107.         else {
  108.             if(prevlen == 1 || shutoff) {
  109.                 Putchar('\n');
  110.                 newlines=0;
  111.             } else
  112.                 Putchar(' ');
  113.         }
  114.         prevlen=len;
  115.         Pipe(buffer);
  116.     }
  117.     return ret;
  118. }
  119.  
  120. int Pipe(char *string)
  121. {
  122.     int white;
  123.     int word;
  124.     int left;
  125.     while(*string) {
  126.         if(!currcol) {
  127.             /* hmmm, pretend we have the leftside already written! */
  128.             left = strlen(leftside);
  129.         }
  130.         else
  131.             left = 0;
  132.  
  133.         white = WhiteCount(string);
  134.         word = WordCount(string + white);
  135.         if(word) {
  136.             /*
  137.              * This is only for white + word
  138.              */
  139.             if(currcol + left + white + word <column) {
  140.                 PutNchar(string, white+word);
  141.             }
  142.             else {
  143.                 Putchar('\n');
  144.                 PutNchar(string + white, word);
  145.             }
  146.         }
  147.         string += white + word;
  148.     }
  149.     return 0;
  150. }
  151.  
  152. int WhiteCount(char *string)
  153. {
  154.     int number=0;
  155.     while(*string && isspace(*string)) {
  156.         string++;
  157.         number++;
  158.     }
  159.     return number;
  160. }
  161.  
  162. int WordCount(char *string)
  163. {
  164.     int number=0;
  165.     while(*string && !isspace(*string)) {
  166.         string++;
  167.         number++;
  168.     }
  169.     return number;
  170. }
  171.  
  172. int Puts(char *string)
  173. {
  174.     while(*string) {
  175.         Putchar(*string);
  176.         string++;
  177.     }
  178.     return 0;
  179. }
  180.  
  181. int Putchar(int letter)
  182. {
  183.     if(letter != '\n') {
  184.         if(!currcol) {
  185.             currcol++;
  186.             Puts(leftside);
  187.         } else if(currcol >= column) {
  188.             putchar('\n');
  189.             currcol=1;
  190.             Puts(leftside);
  191.         } else
  192.             currcol++;
  193.     }
  194.     else {
  195.         currcol = 0;
  196.     }
  197.     if(letter) {
  198.         putchar(letter);
  199.     }
  200.     else
  201.         currcol--;
  202.     return 0;
  203. }
  204.  
  205. int PutNchar(char *string, int N)
  206. {
  207.     while(N--) {
  208.         Putchar(*string++);
  209.     }
  210.     return 0;
  211. }
  212.